﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Com.Crc.ResManage.BLL.EquipManage;
using Com.Crc.PoJo.Model.UserBoard;
using Com.Crc.PoJo.Model.Resource;
using Com.App_Code;

namespace Com.Crc.ResManage.Web.EquipManage
{
    public partial class BroadBandManageSubUserBoardSub : BasePage
    {
        BroadBandManageUserBoardBLL bll;

        public string EquipID
        {
            get
            {
                if (ViewState["EquipID"] != null)
                {
                    return ViewState["EquipID"].ToString();
                }

                return "";
            }
            set
            {
                ViewState["EquipID"] = value;
            }
        }

        public string UserBoardID
        {
            get
            {
                if (ViewState["UserBoardID"] != null)
                {
                    return ViewState["UserBoardID"].ToString();
                }

                return "";
            }
            set
            {
                ViewState["UserBoardID"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitControl();
            }
        }

        private void InitControl()
        {
            if (Request.QueryString["equipid"] != null)
            {
                EquipID = Request.QueryString["equipid"].ToString();
            }

            if (Request.QueryString["userboardid"] != null)
            {
                UserBoardID = Request.QueryString["userboardid"].ToString();
            }

            InitControlManagerBLL bllManager = new InitControlManagerBLL();
            DataTable dtUserBoardType = bllManager.QueryCommonDicInfo(Dictionary.COMMON_DIC_TYPE.RESOURCE_WLAN_USER_BOARD_TYPE);
            this.rblUserBoradType.DataSource = dtUserBoardType;
            this.rblUserBoradType.DataTextField = "s_name";
            this.rblUserBoradType.DataValueField = "n_id";
            this.rblUserBoradType.DataBind();

            if (Request.QueryString["optype"] != null && Request.QueryString["optype"].ToString() == "0")
            {
                LoadUserBoardData();
            }
        }

        private void LoadUserBoardData()
        {
            bll = new BroadBandManageUserBoardBLL();
            DataTable dt = bll.QueryWLANUserBoardData("", "", "", UserBoardID);

            this.txtBeginSlot.Text = dt.Rows[0]["N_TREN_S"].ToString();
            this.txtEndSlot.Text = dt.Rows[0]["N_TREN_E"].ToString();
            this.txtBeginVlan.Text = dt.Rows[0]["N_VLAN_S"].ToString();
            this.txtEndVlan.Text = dt.Rows[0]["N_VLAN_E"].ToString();
            this.txtPortBegin.Text = dt.Rows[0]["N_PORT_S"].ToString();
            this.txtPortEnd.Text = dt.Rows[0]["N_PORT_E"].ToString();
            this.txtUserBoardNum.Text = dt.Rows[0]["N_CAP"].ToString();
            this.rblUserBoradType.SelectedIndex = this.rblUserBoradType.Items.IndexOf(
                this.rblUserBoradType.Items.FindByValue(dt.Rows[0]["N_TYPE"].ToString()));
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            bll = new BroadBandManageUserBoardBLL();

            WLANUserBoardEntity wlanUserBoardData = new WLANUserBoardEntity();
            wlanUserBoardData.S_NAME = "";
            wlanUserBoardData.N_EXCHANGE_ID = long.Parse(EquipID);
            wlanUserBoardData.N_CAP = long.Parse(txtUserBoardNum.Text.Trim());
            wlanUserBoardData.N_PORT_S = long.Parse(txtPortBegin.Text.Trim());
            wlanUserBoardData.N_PORT_E = long.Parse(txtPortEnd.Text.Trim());
            wlanUserBoardData.N_TREN_S = long.Parse(txtBeginSlot.Text.Trim());
            wlanUserBoardData.N_TREN_E = long.Parse(txtEndSlot.Text.Trim());
            wlanUserBoardData.N_VLAN_S = long.Parse(txtBeginVlan.Text.Trim());
            wlanUserBoardData.N_VLAN_E = long.Parse(txtEndVlan.Text.Trim());
            wlanUserBoardData.N_TYPE = long.Parse(this.rblUserBoradType.Items[this.rblUserBoradType.SelectedIndex].Value);

            if (Request.QueryString["optype"] != null && Request.QueryString["optype"].ToString() == "0")
            {
                //修改
                wlanUserBoardData.N_ID = long.Parse(this.UserBoardID);
                bll.UpdateWLANUserBoardData(wlanUserBoardData);
                base.ShowAlertForUpdatePanel("用户板修改成功！");
            }
            else
            {
                //新增
                bll.AddWLANUserBoardData(wlanUserBoardData);
                base.ShowAlertForUpdatePanel("用户板新增成功！");
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            this.txtBeginSlot.Text = "";
            this.txtEndSlot.Text = "";
            this.txtBeginVlan.Text = "";
            this.txtEndVlan.Text = "";
            this.txtPortBegin.Text = "";
            this.txtPortEnd.Text = "";
            this.txtUserBoardNum.Text = "";
        }
    }
}